package part15;

import java.lang.reflect.Method;

/**
 * @author wcj
 * @Date 2021/5/30 21:18
 * @Version 1.0
 */
public class Part1538 {

    public static void main(String[] args) {
        CommunicateReflect.perform(new Mime());
        CommunicateReflect.perform(new SmartDog());
    }
}

class Mime {
    public void walkAgainstWind() {
    }

    public void sit() {
        System.out.println("Mime site");
    }

    public void pushInvisibleWalls() {
    }

    @Override
    public String toString() {
        return "Mime";
    }
}

class SmartDog {
    public void speak() {
        System.out.println("Woof!");
    }

    public void sit() {
        System.out.println("sitting");
    }

    public void reProduce() {
    }
}

/**
 * 通过反射机制我们可以实现更泛化的代码
 */
class CommunicateReflect {
    public static void perform(Object obj) {
        Class<?> a = obj.getClass();
        try {
            Method speak = a.getMethod("speak");
            speak.invoke(obj);
        } catch (Exception e) {
            System.out.println(e);
        }

        try {
            Method sit = a.getMethod("sit");
            sit.invoke(obj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
